home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / isconn_1 / rect.cls < prev    next >
Text File  |  1999-08-31  |  6KB  |  230 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CRect"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14.  
  15. Option Explicit
  16.  
  17.  
  18. #Const ADD_LINE_LOGIC = True
  19.  
  20. Private Type POINTAPI
  21.     X As Long
  22.     Y As Long
  23. End Type
  24.  
  25. Private Type RECT
  26.     Left As Long
  27.     Top As Long
  28.     Right As Long
  29.     Bottom As Long
  30. End Type
  31.  
  32. Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
  33. Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
  34.  
  35. Private m_Rect As RECT
  36.  
  37. #If ADD_LINE_LOGIC Then
  38.  
  39. '
  40. Private Const SWAP_NONE = &H0
  41. Private Const SWAP_X = &H1
  42. Private Const SWAP_Y = &H2
  43. Private m_fRectSwap As Integer
  44.  
  45. #End If
  46.  
  47. Public Property Let Left(NewLeft As Long)
  48.     m_Rect.Left = NewLeft
  49. End Property
  50.  
  51. Public Property Get Left() As Long
  52.     Left = m_Rect.Left
  53. End Property
  54.  
  55. Public Property Let Top(NewTop As Long)
  56.     m_Rect.Top = NewTop
  57. End Property
  58.  
  59. Public Property Get Top() As Long
  60.     Top = m_Rect.Top
  61. End Property
  62.  
  63. Public Property Let Right(NewRight As Long)
  64.     m_Rect.Right = NewRight
  65. End Property
  66.  
  67. Public Property Get Right() As Long
  68.     Right = m_Rect.Right
  69. End Property
  70.  
  71. Public Property Let Bottom(NewBottom As Long)
  72.     m_Rect.Bottom = NewBottom
  73. End Property
  74.  
  75. Public Property Get Bottom() As Long
  76.     Bottom = m_Rect.Bottom
  77. End Property
  78.  
  79. Public Property Let Width(NewWidth As Long)
  80.     m_Rect.Right = m_Rect.Left + NewWidth
  81. End Property
  82.  
  83. Public Property Get Width() As Long
  84.     Width = m_Rect.Right - m_Rect.Left
  85. End Property
  86.  
  87. Public Property Let Height(NewHeight As Long)
  88.     m_Rect.Bottom = m_Rect.Top + NewHeight
  89. End Property
  90.  
  91. Public Property Get Height() As Long
  92.     Height = m_Rect.Bottom - m_Rect.Top
  93. End Property
  94.  
  95. Public Sub SetRectToCtrl(ctl As Control)
  96. On Error Resume Next
  97. #If ADD_LINE_LOGIC Then
  98.  
  99.     'Reset swap flags
  100.     m_fRectSwap = SWAP_NONE
  101.     If TypeOf ctl Is Line Then
  102.         m_Rect.Left = ctl.x1
  103.         m_Rect.Top = ctl.y1
  104.         m_Rect.Right = ctl.x2
  105.         m_Rect.Bottom = ctl.y2
  106.         'Need valid rect for hit testing but
  107.         'must swap back in SetCtrlToRect
  108.         If m_Rect.Left > m_Rect.Right Then
  109.             m_fRectSwap = m_fRectSwap Or SWAP_X
  110.         End If
  111.         If m_Rect.Top > m_Rect.Bottom Then
  112.             m_fRectSwap = m_fRectSwap Or SWAP_Y
  113.         End If
  114.         'Normalize if needed
  115.         If m_fRectSwap <> SWAP_NONE Then
  116.             NormalizeRect
  117.         End If
  118.     Else
  119.         m_Rect.Left = ctl.Left
  120.         m_Rect.Top = ctl.Top
  121.         m_Rect.Right = ctl.Left + ctl.Width
  122.         m_Rect.Bottom = ctl.Top + ctl.Height
  123.     End If
  124.  
  125. #Else
  126.  
  127.     m_Rect.Left = ctl.Left
  128.     m_Rect.Top = ctl.Top
  129.     m_Rect.Right = ctl.Left + ctl.Width
  130.     m_Rect.Bottom = ctl.Top + ctl.Height
  131.  
  132. #End If
  133.  
  134. End Sub
  135.  
  136. Public Sub SetCtrlToRect(ctl As Control)
  137. On Error Resume Next
  138. #If ADD_LINE_LOGIC Then
  139.  
  140.     If TypeOf ctl Is Line Then
  141.         'Restore normalized rectangle if needed
  142.         If m_fRectSwap And SWAP_X Then
  143.             ctl.x1 = m_Rect.Right
  144.             ctl.x2 = m_Rect.Left
  145.         Else
  146.             ctl.x1 = m_Rect.Left
  147.             ctl.x2 = m_Rect.Right
  148.         End If
  149.         If m_fRectSwap And SWAP_Y Then
  150.             ctl.y1 = m_Rect.Bottom
  151.             ctl.y2 = m_Rect.Top
  152.         Else
  153.             ctl.y1 = m_Rect.Top
  154.             ctl.y2 = m_Rect.Bottom
  155.         End If
  156.         'Force to valid rectangle
  157.         NormalizeRect
  158.     Else
  159.         'Force to valid rectangle
  160.         NormalizeRect
  161.         ctl.Move m_Rect.Left, m_Rect.Top, Width, Height
  162.     End If
  163.  
  164. #Else
  165.  
  166.     'Force to valid rectangle
  167.     NormalizeRect
  168.     ctl.Move m_Rect.Left, m_Rect.Top, Width, Height
  169.  
  170. #End If
  171.  
  172. End Sub
  173.  
  174. Public Sub ScreenToTwips(ctl As Object)
  175. On Error Resume Next
  176.     Dim pt As POINTAPI
  177.  
  178.     pt.X = m_Rect.Left
  179.     pt.Y = m_Rect.Top
  180.     ScreenToClient ctl.Parent.hwnd, pt
  181.     m_Rect.Left = pt.X * Screen.TwipsPerPixelX
  182.     m_Rect.Top = pt.Y * Screen.TwipsPerPixelX
  183.     pt.X = m_Rect.Right
  184.     pt.Y = m_Rect.Bottom
  185.     ScreenToClient ctl.Parent.hwnd, pt
  186.     m_Rect.Right = pt.X * Screen.TwipsPerPixelX
  187.     m_Rect.Bottom = pt.Y * Screen.TwipsPerPixelX
  188. End Sub
  189.  
  190. Public Sub TwipsToScreen(ctl As Object)
  191. On Error Resume Next
  192.     Dim pt As POINTAPI
  193.  
  194.     pt.X = m_Rect.Left / Screen.TwipsPerPixelX
  195.     pt.Y = m_Rect.Top / Screen.TwipsPerPixelX
  196.     ClientToScreen ctl.Parent.hwnd, pt
  197.     m_Rect.Left = pt.X
  198.     m_Rect.Top = pt.Y
  199.     pt.X = m_Rect.Right / Screen.TwipsPerPixelX
  200.     pt.Y = m_Rect.Bottom / Screen.TwipsPerPixelX
  201.     ClientToScreen ctl.Parent.hwnd, pt
  202.     m_Rect.Right = pt.X
  203.     m_Rect.Bottom = pt.Y
  204. End Sub
  205.  
  206. Public Sub NormalizeRect()
  207.     Dim nTemp As Long
  208.  
  209.     If m_Rect.Left > m_Rect.Right Then
  210.         nTemp = m_Rect.Right
  211.         m_Rect.Right = m_Rect.Left
  212.         m_Rect.Left = nTemp
  213.     End If
  214.     If m_Rect.Top > m_Rect.Bottom Then
  215.         nTemp = m_Rect.Bottom
  216.         m_Rect.Bottom = m_Rect.Top
  217.         m_Rect.Top = nTemp
  218.     End If
  219. End Sub
  220.  
  221. Public Function PtInRect(X As Single, Y As Single) As Integer
  222.     If X >= m_Rect.Left And X < m_Rect.Right And _
  223.         Y >= m_Rect.Top And Y < m_Rect.Bottom Then
  224.         PtInRect = True
  225.     Else
  226.         PtInRect = False
  227.     End If
  228. End Function
  229.  
  230.